package com.banciyuan.bcywebview.biz.zanlist;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.ZanListItem;

/**
 * Created by hy on 2015/4/29.
 */
public class ZanListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<ZanListItem> data;

    public ZanListAdapter(Context context, List<ZanListItem> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ViewZanEmpty(View.inflate(context, R.layout.zanlist_empty_view, null));
        } else {
            return new ViewItem(View.inflate(context, R.layout.gridcircle_item, null));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0:
                ViewZanEmpty viewZanEmpty = (ViewZanEmpty) holder;
                if (getItemCount() == 1) {
                    RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT);
                    viewZanEmpty.itemView.setLayoutParams(layoutParams);
                    viewZanEmpty.itemView.setVisibility(View.VISIBLE);
                } else {
                    RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, 1);
                    viewZanEmpty.itemView.setLayoutParams(layoutParams);
                    viewZanEmpty.itemView.setVisibility(View.GONE);
                }
                break;
            case 1:
                renderZan((ViewItem) holder, position - 1);
                break;
        }
    }

    private void renderZan(ViewItem viewHolder, int position) {
        ZanListItem mItem = data.get(position);
        viewHolder.itemCover.setVisibility(View.GONE);
        viewHolder.priceIcon.setVisibility(View.GONE);
        viewHolder.itemShadow.setVisibility(View.GONE);
        viewHolder.itemTitle.setVisibility(View.GONE);
        viewHolder.itemIntro.setVisibility(View.GONE);
        viewHolder.itemLabel.setVisibility(View.GONE);
        viewHolder.itemTag.setVisibility(View.GONE);
        viewHolder.videoIcon.setVisibility(View.GONE);
        viewHolder.itemShadow.setAlpha(0.8f);
        if (StringUtil.notNullEqual(mItem.getType(), "svideo")) {
            viewHolder.itemShadow.setVisibility(View.GONE);
            viewHolder.videoIcon.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(mItem.getImg_src())) {
                viewHolder.itemCover.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(mItem.getImg_src(), viewHolder.itemCover, ImageOptions.options);
            }
        } else if (StringUtil.notNullEqual(mItem.getType(), "larticle")) {
            viewHolder.itemShadow.setVisibility(View.VISIBLE);
            viewHolder.itemLabel.setVisibility(View.VISIBLE);
            viewHolder.itemLabel.setText(context.getString(R.string.larticle));
            if (!TextUtils.isEmpty(mItem.getTitle())) {
                viewHolder.itemTitle.setVisibility(View.VISIBLE);
                viewHolder.itemTitle.setText(mItem.getTitle());
            }
            if (!TextUtils.isEmpty(mItem.getImg_src())) {
                viewHolder.itemCover.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(mItem.getImg_src(), viewHolder.itemCover, ImageOptions.options);
            }
        } else {
            if (!TextUtils.isEmpty(mItem.getImg_src())) {
                viewHolder.itemShadow.setVisibility(View.GONE);
                viewHolder.itemCover.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(mItem.getImg_src(), viewHolder.itemCover, ImageOptions.options);
            } else {
                if (!TextUtils.isEmpty(mItem.getPlain())) {
                    viewHolder.itemIntro.setVisibility(View.VISIBLE);
                    viewHolder.itemIntro.setText(Html.fromHtml(mItem.getPlain()));
                }
                viewHolder.itemCover.setVisibility(View.GONE);
                viewHolder.itemShadow.setVisibility(View.VISIBLE);
            }
        }
        OnClick onClick = new OnClick(position);
        viewHolder.itemView.setOnClickListener(onClick);
    }

    private class ViewItem extends RecyclerView.ViewHolder {
        ImageView itemCover;
        TextView itemTitle;
        TextView itemIntro;
        TextView itemTag;
        TextView itemLabel;
        TextView priceIcon;
        View itemShadow;
        View videoIcon;

        public ViewItem(View convertView) {
            super(convertView);
            itemCover = (ImageView) convertView.findViewById(R.id.item_cover);
            itemTitle = (TextView) convertView.findViewById(R.id.item_title);
            itemIntro = (TextView) convertView.findViewById(R.id.item_intro);
            itemTag = (TextView) convertView.findViewById(R.id.item_tag);
            itemLabel = (TextView) convertView.findViewById(R.id.item_label);
            priceIcon = (TextView) convertView.findViewById(R.id.item_price);
            itemShadow = convertView.findViewById(R.id.item_shadow);
            videoIcon = convertView.findViewById(R.id.item_video_icon);
        }
    }

    private class ViewZanEmpty extends RecyclerView.ViewHolder {

        public ViewZanEmpty(View itemView) {
            super(itemView);
        }
    }

    class OnClick implements View.OnClickListener {

        int position;

        public OnClick(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if (position < data.size()) {
                ZanListItem item = data.get(position);
                switch (v.getId()) {
                    case R.id.write_item_one:
                        if (!TextUtils.isEmpty(item.getCp_id())) {
                            DetailDataHelper.gotoCosDetail(context, item.getRp_id(), item.getCp_id(), false);
                        } else if (!TextUtils.isEmpty(item.getDp_id())) {
                            DetailDataHelper.gotoDrawerDetail(context, item.getRp_id(), item.getDp_id(), false);
                        } else {
                            DetailDataHelper.gotoDailyDetail(context, item.getUd_id(), false);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}
